﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Module_Ribbon_Detail : DetailPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        base.initializeWebForm();

        if (!IsPostBack)
        {
            Genesis.Client.Module.Classes.ModuleProvider moduleProvider = new Genesis.Client.Module.Classes.ModuleProvider();

            System.Data.DataTable applicationTable = moduleProvider.ApplicationGetList();

            applicationDropDownList.DataSource = applicationTable;
            applicationDropDownList.DataTextField = "ApplicationName";
            applicationDropDownList.DataValueField = "ApplicationGuid";

            applicationDropDownList.DataBind();

            Guid _ribbonGuid = Guid.Empty;

            if (Request.QueryString["RibbonGuid"] != null)
            {
                _ribbonGuid = new Guid(Request.QueryString["RibbonGuid"]);
            }

            if (_ribbonGuid != Guid.Empty)
            {
                System.Data.DataTable ribbonTable = moduleProvider.RibbonGetByRibbonGuid(_ribbonGuid);

                if (ribbonTable.Rows.Count == 1)
                {
                    System.Data.DataRow ribbonRow = ribbonTable.Rows[0];

                    ribbonIdTextBox.Text = System.Convert.ToString(ribbonRow["RibbonId"]);
                    ribbonGuidTextBox.Text = System.Convert.ToString(ribbonRow["RibbonGuid"]);
                    orderTextBox.Text = System.Convert.ToString(ribbonRow["RibbonOrder"]);

                    ribbonNameTextBox.Text = System.Convert.ToString(ribbonRow["RibbonName"]);

                    string _applicationGuidString = System.Convert.ToString(ribbonRow["ApplicationGuid"]);

                    foreach (ListItem applicationItem in applicationDropDownList.Items)
                    {
                        if (applicationItem.Value == _applicationGuidString)
                        {
                            applicationItem.Selected = true;
                            break;
                        }
                    }
                }
            }

            Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

            System.Data.DataTable roleTable = securityProvider.RoleGetList();

            groupDropDownList.DataSource = roleTable;
            groupDropDownList.DataTextField = "RoleName";
            groupDropDownList.DataValueField = "RoleGuid";

            groupDropDownList.DataBind();

            System.Data.DataTable ribbonRoleTable = securityProvider.RoleGetByRibbonGuid(_ribbonGuid);

            groupListBox.DataSource = ribbonRoleTable;
            groupListBox.DataTextField = "RoleName";
            groupListBox.DataValueField = "RoleGuid";

            groupListBox.DataBind();

            switch (formMode)
            {
                case FormMode.Create:
                    ribbonNameTextBox.Enabled = true;
                    orderTextBox.Enabled = true;

                    ribbonNameTextBox.ReadOnly = false;
                    orderTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Delete:
                    ribbonNameTextBox.Enabled = true;
                    orderTextBox.Enabled = true;

                    ribbonNameTextBox.ReadOnly = true;
                    orderTextBox.ReadOnly = true;

                    deleteImageButton.Visible = true;
                    break;
                case FormMode.Edit:
                    ribbonNameTextBox.Enabled = true;
                    orderTextBox.Enabled = true;

                    ribbonNameTextBox.ReadOnly = false;
                    orderTextBox.ReadOnly = false;

                    deleteImageButton.Visible = false;
                    break;
                case FormMode.Default:
                    ribbonNameTextBox.Enabled = true;
                    orderTextBox.Enabled = true;

                    ribbonNameTextBox.ReadOnly = true;
                    orderTextBox.ReadOnly = true;

                    deleteImageButton.Visible = false;
                    break;
            }
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect(this.buildForwardUrl("Default.aspx"));
    }
    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
    {
        Genesis.Client.Module.Classes.ModuleProvider moduleProvider = new Genesis.Client.Module.Classes.ModuleProvider();

        if (formMode == FormMode.Edit)
        {
            if (!moduleProvider.RibbonUpdate(
                System.Convert.ToInt32(ribbonIdTextBox.Text),
                new Guid(ribbonGuidTextBox.Text),
                new Guid(applicationDropDownList.SelectedValue),
                ribbonNameTextBox.Text,
                System.Convert.ToInt32(orderTextBox.Text)))
            {
                Response.Write("alert(\"An error occured while updating the database\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
        else if (formMode == FormMode.Create)
        {
            if (moduleProvider.RibbonInsert(
                Guid.NewGuid(),
                new Guid(applicationDropDownList.SelectedValue),
                ribbonNameTextBox.Text,
                System.Convert.ToInt32(orderTextBox.Text)) == 0)
            {
                Response.Write("alert(\"An error occured while creating database entry\");");
            }
            else
            {
                Response.Redirect(this.buildForwardUrl("Default.aspx"));
            }
        }
    }
    protected void groupListBox_SelectedIndexChanged(object sender, EventArgs e)
    {
        UnsubscribeButton.Enabled = groupListBox.SelectedIndex != -1;
    }
    protected void subscribeButton_Click(object sender, EventArgs e)
    {
        Guid _ribbonGuid = Guid.Empty;

        if (Request.QueryString["RibbonGuid"] != null)
        {
            _ribbonGuid = new Guid(Request.QueryString["RibbonGuid"]);
        }

        if (_ribbonGuid != Guid.Empty)
        {
            Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

            securityProvider.RibbonRoleInsert(_ribbonGuid, new Guid(groupDropDownList.SelectedValue));

            System.Data.DataTable ribbonRoleTable = securityProvider.RoleGetByRibbonGuid(_ribbonGuid);

            groupListBox.DataSource = ribbonRoleTable;
            groupListBox.DataTextField = "RoleName";
            groupListBox.DataValueField = "RoleGuid";

            groupListBox.DataBind();
        }
    }
    protected void UnsubscribeButton_Click(object sender, EventArgs e)
    {
        Guid _ribbonGuid = Guid.Empty;

        if (Request.QueryString["RibbonGuid"] != null)
        {
            _ribbonGuid = new Guid(Request.QueryString["RibbonGuid"]);
        }

        if (_ribbonGuid != Guid.Empty)
        {
            Genesis.Client.Security.Classes.SecurityProvider securityProvider = new Genesis.Client.Security.Classes.SecurityProvider();

            securityProvider.RibbonRoleDelete(_ribbonGuid, new Guid(groupListBox.SelectedValue));

            System.Data.DataTable ribbonRoleTable = securityProvider.RoleGetByRibbonGuid(_ribbonGuid);

            groupListBox.DataSource = ribbonRoleTable;
            groupListBox.DataTextField = "RoleName";
            groupListBox.DataValueField = "RoleGuid";

            groupListBox.DataBind();

            UnsubscribeButton.Enabled = false;
        }
    }
}
